package com.atguigu.yygh.hosp.controller.admin;


import com.atguigu.yygh.common.exception.YyghException;
import com.atguigu.yygh.common.result.R;
import com.atguigu.yygh.common.util.MD5;
import com.atguigu.yygh.hosp.service.HospitalSetService;
import com.atguigu.yygh.model.hosp.HospitalSet;
import com.atguigu.yygh.vo.hosp.HospitalSetQueryVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Random;

/**
 * <p>
 * 医院设置表 前端控制器
 * </p>
 *
 * @author atguigu
 * @since 2022-08-20
 */
@RestController
@Api(tags = "医院设置信息")
@RequestMapping("/admin/hosp/hospitalSet")
public class HospitalSetController {

    @Autowired
    private HospitalSetService hospitalSetService;

    @ApiOperation(value = "查询所有医院设置信息")
    @GetMapping("findAll")
    public R findAll() {
        List<HospitalSet> list = hospitalSetService.list();
        return R.ok().data("items", list);
    }

    // 逻辑删除
    @ApiOperation(value = "根据医院设置id删除医院设置信息")
    @DeleteMapping("/deleteById/{id}")
    public R removeById(@ApiParam(value = "医院设置id", name = "id", required = true) @PathVariable("id")String id) {
        hospitalSetService.removeById(id);
        return R.ok();
    }

    // 带条件查询的分页
    @ApiOperation(value = "医院设置信息的带条件查询的分页")
    @PostMapping("/page/{pageNum}/{pageSize}")
    public R getPageInfo(@ApiParam(name = "pageNum", value = "当前页码") @PathVariable("pageNum")Integer pageNum,
                         @ApiParam(name = "pageSize", value = "每页显示几条数据") @PathVariable("pageSize")Integer pageSize,
                         @RequestBody HospitalSetQueryVo hospitalSetQueryVo) {
        Page<HospitalSet> page = new Page<HospitalSet>(pageNum, pageSize);
        QueryWrapper<HospitalSet> queryWrapper = new QueryWrapper<>();
        // 条件查润
        if(!StringUtils.isEmpty(hospitalSetQueryVo.getHosname())) {
            queryWrapper.like("hosname", hospitalSetQueryVo.getHosname());
        }

        if(!StringUtils.isEmpty(hospitalSetQueryVo.getHoscode())) {
            queryWrapper.eq("hoscode", hospitalSetQueryVo.getHoscode());
        }
        hospitalSetService.page(page, queryWrapper);
        // 返回数据
        long total = page.getTotal();
        return R.ok().data("total", total).data("rows", page.getRecords());
    }

    // 新增
    @ApiOperation(value = "医院设置信息新增接口")
    @PostMapping("/save")
    public R save(@RequestBody HospitalSet hospitalSet) {
        // 设置状态： 给出默认状态： 1正常0不正常
        hospitalSet.setStatus(1);
        // 设置密钥：当前时间戳+随机数
        Random random = new Random();
        hospitalSet.setSignKey(MD5.encrypt(System.currentTimeMillis()+""+random.nextInt(1000)));
        // 保存
        hospitalSetService.save(hospitalSet);
        return R.ok();
    }

    // 修改的回显数据
    @ApiOperation(value = "修改的回显数据")
    @GetMapping("/detail/{id}")
    public R detail(@ApiParam(name = "id", value = "查询id")
                        @PathVariable("id") Integer id) {
        return R.ok().data("item", hospitalSetService.getById(id));
    }

    // 真实修改
    @ApiOperation(value = "医院设置信息修改接口")
    @PutMapping("/update")
    public R update(@RequestBody HospitalSet hospitalSet) {
        hospitalSetService.updateById(hospitalSet);
        return R.ok();
    }

    // 修改锁定状态
    @ApiOperation(value = "医院设置信息修改锁定状态接口")
    @PutMapping("/status/{id}/{status}")
    public R updateStatus(@PathVariable("id") Long id, @PathVariable("status")Integer status){
        HospitalSet hospitalSet = hospitalSetService.getById(id);
        hospitalSet.setStatus(status);
        hospitalSetService.updateById(hospitalSet);
        return R.ok();
    }

    // 批量删除
    @ApiOperation(value = "医院设置信息批量删除接口")
    @DeleteMapping("/delete")
    public R batchDelete(@RequestBody List<Integer> ids) {
        hospitalSetService.removeByIds(ids);
        return R.ok();
    }
}

